from control.models import *
from literature.models import *
import pandas as pd
from django.db import IntegrityError, transaction
import json
from tqdm import tqdm
import traceback
from datetime import datetime
from basic_component_related_management.models import *
from dataset.models import *
from collections import defaultdict


def init_dippr_const_data():
    super_admin_entity = User.objects.filter(user_account="superadmin").first()
    unit_file_path = "files/basic_data_init_final_v2/dippr/3.Const Properties Details-20250306R1.xlsx"
    # unit_file_path = "files/basic_data_init_final_v2/dippr/3.Const Properties Details-20250306R1 -test.xlsx"
    # unit_file_path = "files/basic_data_init_final_v2/DIPPR对齐表/3.constTest.xlsx"
    sheet_name = "Sheet1"
    df = pd.read_excel(unit_file_path, sheet_name=sheet_name, header=0, dtype=str)
    start_row = 0
    end_row = 54
    # end_row = 13
    num_columns = 15
    selected_data = df.iloc[start_row:, :num_columns].fillna("")
    # print(selected_data)
    dippr_attribute_entity = Attribute.objects.filter(name_en="nDipprID").first()
    if dippr_attribute_entity is None:
        print("dippr attribute not exist")
        return
    unit_1_entity = Unit.objects.filter(sei_id="U-0-1").first()
    if unit_1_entity is None:
        print("unit symbol 1 not exist")
        return
    # 获取note
    note_json_path = "files/basic_data_init_final_v2/dippr_note.json"
    with open(note_json_path, "r", encoding="utf-8") as file:
        note_dict = json.load(file)
    # 获取dippr与ref对齐
    dippr_ref_2_ref_id_dict_path = (
        "files/basic_data_init_final_v2/dippr_id_2_ref_id.json"
    )
    with open(dippr_ref_2_ref_id_dict_path, "r", encoding="utf-8") as file:
        dippr_ref_2_ref_id_dict = json.load(file)

    # 数据类型：Unknown
    data_present_unknow_entity = DataPresentWay.objects.filter(name="Unknown").first()
    if data_present_unknow_entity == None:
        data_present_unknow_entity = DataPresentWay.objects.create(
            name="Unknown", creator=super_admin_entity, last_editor=super_admin_entity
        )
    purpose_others_entity = ExpPurpose.objects.filter(name="others").first()
    if purpose_others_entity == None:
        purpose_others_entity = ExpPurpose.objects.create(
            name="others",
            creator=super_admin_entity,
            last_editor=super_admin_entity,
        )

    # for index, row in tqdm(selected_data.iterrows(), desc="Processing dataset data"):
    for index, row in selected_data.iterrows():
        try:
            if index < -1:
                continue

            component_id = row["DIPPR ID"]
            if component_id == "":
                raise Exception()
            property_abb = row["ePropAbbreviation"]
            if property_abb == "":
                raise Exception()
            phase_symbol = row["sPhaseSymbol"]
            value = row["Value"]
            unit_id = row["nUnitIDCombine"]
            note_id = row["Note"]
            dippr_ref_id = row["Reference"]
            data_type = row["Data Type"]
            error = row["Error"]
            source = row["Source"]
            Acceptance = row["Acceptance"]

            # 查找组分
            component_entity_attribute_map = ComponentAttributeValue.objects.filter(
                attribute=dippr_attribute_entity, attribute_value_char=component_id
            ).first()
            if component_entity_attribute_map is None:
                print("error at component dippr id :", component_id)
                print("error at index:", index)
                continue
            else:
                component_entity = component_entity_attribute_map.componentidentifier
            # 查物性
            property_entity = Property.objects.filter(abbreviation=property_abb).first()
            if property_entity is None:
                raise Exception()
            # 查相态
            if phase_symbol == "":
                phase_entity = None
            else:
                phase_entity = Phase.objects.filter(abbreviation=phase_symbol).first()
                if phase_entity is None:
                    raise Exception()
            # 查单位
            if unit_id == "":
                unit_entity = unit_1_entity
            else:
                unit_entity = Unit.objects.filter(sei_id=unit_id).first()
            # 获取note
            description = ""
            comment = ""
            if note_id != "":
                note1 = note_dict.get(note_id, "")
                if note1 != "":
                    description += note1 + "\n"
                    comment += note1 + "\n"

            if source != "":
                comment += "source:" + source + "\n"
            if Acceptance != "":
                comment += "Acceptance" + Acceptance

            # 定义数据类型
            if data_type == "":
                # 数据类型为Unknown
                data_present_way_entity = data_present_unknow_entity
            else:
                if data_type == "Not Specified":
                    data_type = "Unspecified"
                if data_type == "Experiment":
                    data_type = "Experimental" 
                data_present_way_entity = DataPresentWay.objects.filter(
                    name=data_type
                ).first()
                if data_present_way_entity == None:
                    data_present_way_entity = DataPresentWay.objects.create(
                        name=data_type,
                        creator=super_admin_entity,
                        last_editor=super_admin_entity,
                    )

            # 获取文献
            if dippr_ref_id == "":
                dippr_reference_entity = None
            else:
                ref_id = dippr_ref_2_ref_id_dict.get(dippr_ref_id, "")
                if ref_id == "":
                    dippr_reference_entity = None
                else:
                    dippr_reference_entity = Literature.objects.filter(
                        liter_code=str(ref_id)
                    ).first()
            # 构造一个数据集名称,名称+物性
            dataset_name = row["Compound"] + "--" + row["Property"]

            ####开始入库
            with transaction.atomic():
                dataset_entity = ExpDataset.objects.create(
                    name=dataset_name,
                    zh_name=dataset_name,
                    description=description,
                    comment=comment,
                    source_liter=dippr_reference_entity,
                    purpose=purpose_others_entity,
                    data_present_way=data_present_way_entity,
                    review_status=1,
                    creator=super_admin_entity,
                    last_editor=super_admin_entity,
                )

                if phase_entity != None:
                    dataset_entity.phases.set([phase_entity])
                # 设置数据集组分
                ExpDatasetComponentMap.objects.create(
                    exp_dataset=dataset_entity, component=component_entity
                )
                # 设置数据集物性
                if error == "":
                    ExpDatasetPropertyMap_entity = ExpDatasetPropertyMap.objects.create(
                        exp_dataset=dataset_entity,
                        property=property_entity,
                        unit=unit_entity,
                    )
                else:
                    ExpDatasetPropertyMap_entity = ExpDatasetPropertyMap.objects.create(
                        exp_dataset=dataset_entity,
                        property=property_entity,
                        unit=unit_entity,
                        measurement_error=error,
                        measurement_error_percent=True,
                    )

                # 只有一个datapoint
                datapoint_entity = DataPoint.objects.create()
                dataset_entity.data_points.set([datapoint_entity])
                datapoint_entity.save()
                # 设置这个datapoint的值
                if error == "":
                    DataPointExpDatasetPropertyMap_entity = (
                        DataPointExpDatasetPropertyMap.objects.create(
                            data_point=datapoint_entity,
                            exp_dataset_property_map=ExpDatasetPropertyMap_entity,
                            value=value,
                        )
                    )
                else:
                    DataPointExpDatasetPropertyMap_entity = (
                        DataPointExpDatasetPropertyMap.objects.create(
                            data_point=datapoint_entity,
                            exp_dataset_property_map=ExpDatasetPropertyMap_entity,
                            value=value,
                            measurement_error_value=error,
                        )
                    )
                if index % 1000 == 0:
                    print("finish index:", index)
        except Exception as e:
            print("eror at:", index)
            traceback.print_exc()
            return
        # print("finish index:",index)


def convert_note_to_json():
    unit_file_path = "files/basic_data_init_final_v2/A079. PCdata基表-7-标注表（20250403R1）-0版.xlsx"
    sheet_name = "Sheet1"
    df = pd.read_excel(unit_file_path, sheet_name=sheet_name, header=0, dtype=str)
    start_row = 0
    end_row = 11989
    # end_row = 13
    num_columns = 3
    selected_data = df.iloc[start_row:, :num_columns].fillna("")
    # print(selected_data)
    note_dict = {}
    for index, row in selected_data.iterrows():
        nNoteId = row["nNoteId"]
        sNote = row["sNote"]
        cnNote = row["备注"]
        note_dict[nNoteId] = cnNote+"("+sNote+")"
    with open("files/basic_data_init_final_v2/dippr_note.json", "w", encoding="utf-8") as json_file:
        json.dump(note_dict, json_file, indent=4)


def convert_dippr_id_2_ref_id_json():
    # unit_file_path = "files/basic_data_init_final_v2/A075. PCdata基表-3-参考文献表（20250321R1）-C版.xlsx"
    # # unit_file_path = "files/basic_data_init_final_v2/基表20250306/A075test.xlsx"
    # sheet_name = "DIPPR文献对齐"
    # df = pd.read_excel(unit_file_path, sheet_name=sheet_name, header=0, dtype=str)
    # start_row = 0
    # end_row = 31813
    # # end_row = 13
    # num_columns = 2
    # selected_data = df.iloc[start_row:end_row, :num_columns].fillna("")
    # # print(selected_data)
    # dippr_id_2_ref_id_json = {}
    # for index, row in selected_data.iterrows():
    #     if row["RefId"] != "" and row["nPCdataCitID"] != "":
    #         dippr_id_2_ref_id_json[row["RefId"]] = row["nPCdataCitID"]
    # with open(
    #     "files/basic_data_init_final_v2/dippr_id_2_ref_id.json", "w"
    # ) as json_file:
    #     json.dump(dippr_id_2_ref_id_json, json_file, indent=4)
    unit_file_path = "files/basic_data_init_final_v2/A075. PCdata基表-3-参考文献表（20250403R1）-0版.xlsx"
    sheet_name = "文献总表"
    df = pd.read_excel(unit_file_path, sheet_name=sheet_name, header=1, dtype=str)
    start_row = 1
    end_row = 124030
    # end_row = 13
    num_columns = 43
    selected_data = df.iloc[start_row:, :num_columns].fillna("")
    dippr_id_2_ref_id_json = {}
    for index, row in selected_data.iterrows():
        if row["DIPPR_ID"] != "" and row["DIPPR_ID"] != "NA":
            dippr_id_2_ref_id_json[row["DIPPR_ID"]] = row["nPCdataCitID"]
    with open(
        "files/basic_data_init_final_v2/dippr_id_2_ref_id.json", "w", encoding="utf-8"
    ) as json_file:
        json.dump(dippr_id_2_ref_id_json, json_file, indent=4)
                    
    
    
    
    


def convert_dippr_const_property_data_to_json():
    unit_file_path = "files/basic_data_init_final_v2/dippr/3.Const Properties Details-20250306R1.xlsx"
    # unit_file_path = "files/basic_data_init_final_v2/DIPPR对齐表/3.constTest.xlsx"
    sheet_name = "Sheet1"
    df = pd.read_excel(unit_file_path, sheet_name=sheet_name, header=0, dtype=str)
    start_row = 0
    end_row = 54
    # end_row = 13
    num_columns = 15
    const_property_data_list = []
    selected_data = df.iloc[start_row:, :num_columns].fillna("")
    for index, row in selected_data.iterrows():
        if row["Acceptance"] != "A":
            continue
        const_property_data_item = {}
        const_property_data_item["com_id"] = row["DIPPR ID"]
        const_property_data_item["prop_abb"] = row["ePropAbbreviation"]
        const_property_data_item["value"] = row["Value"]
        const_property_data_item["unit_id"] = row["nUnitIDCombine"]
        const_property_data_list.append(const_property_data_item)

    with open(
        "files/basic_data_init_final_v2/const_property_data_list.json", "w"
    ) as json_file:
        json.dump(const_property_data_list, json_file, indent=4)


def init_dippr_const_property():
    super_admin_entity = User.objects.filter(user_account="superadmin").first()

    note_json_path = "files/basic_data_init_final_v2/const_property_data_list.json"
    with open(note_json_path, "r", encoding="utf-8") as file:
        const_property_data_list = json.load(file)

    unit_1_entity = Unit.objects.filter(sei_id="U-0-1").first()
    if unit_1_entity is None:
        print("unit symbol 1 not exist")
        return

    # 先将物性全部创建到常量物性里面
    prop_abb_set = set()
    for const_property_item in const_property_data_list:
        prop_abb_set.add(const_property_item["prop_abb"])
    prop_abb_list = list(prop_abb_set)
    const_property_entity_list = []
    for prop_abb in prop_abb_list:
        property_entity = Property.objects.filter(abbreviation=prop_abb).first()
        if property_entity == None:
            print("error property abb:", prop_abb)
        const_property_entity = ComponentConstProperty(
            name=property_entity.name,
            name_en=property_entity.name_en,
            abbreviation=property_entity.abbreviation,
            quantity=property_entity.quantity,
            data_type="float",
            common_used=True,
            is_show_in_detail=False,
            description=property_entity.description,
            description_en=property_entity.description_en,
            comment=property_entity.comment,
            create_person=super_admin_entity,
            modify_person=super_admin_entity,
        )
        const_property_entity_list.append(const_property_entity)
    try:
        with transaction.atomic():
            ComponentConstProperty.objects.bulk_create(const_property_entity_list)
    except Exception as e:
        traceback.print_exc()


def init_dippr_const_property_data():
    super_admin_entity = User.objects.filter(user_account="superadmin").first()

    note_json_path = "files/basic_data_init_final_v2/const_property_data_list.json"
    with open(note_json_path, "r", encoding="utf-8") as file:
        const_property_data_list = json.load(file)

    unit_1_entity = Unit.objects.filter(sei_id="U-0-1").first()
    if unit_1_entity is None:
        print("unit symbol 1 not exist")
        return
    dippr_attribute_entity = Attribute.objects.filter(name_en="nDipprID").first()
    if dippr_attribute_entity is None:
        print("dippr attribute not exist")
        return

    # 先获取所有的应用物性并做映射
    const_property_entity_list = ComponentConstProperty.objects.filter(valid_flag=True)
    const_property_abb_entity_map = {}
    for const_property_entity in const_property_entity_list:
        const_property_abb_entity_map[const_property_entity.abbreviation] = (
            const_property_entity
        )

    # 获取所有的单位并作映射
    unit_entity_list = Unit.objects.filter(valid_flag=True)
    unit_sei_id_entity_map = {}
    for unit_entity in unit_entity_list:
        if unit_entity.sei_id != "":
            unit_sei_id_entity_map[unit_entity.sei_id] = unit_entity

    blocks = []
    current_block = []
    block_size = 1000
    for item in tqdm(const_property_data_list, desc="Processing const property data"):
        component_entity_attribute_map = ComponentAttributeValue.objects.filter(
            attribute=dippr_attribute_entity, attribute_value_char=item["com_id"]
        ).first()
        if component_entity_attribute_map is None:
            print("error at component dippr id :", item["com_id"])
            continue
        else:
            component_entity = component_entity_attribute_map.componentidentifier
        const_property_entity = const_property_abb_entity_map[item["prop_abb"]]
        if item["unit_id"] == "":
            unit_entity = unit_1_entity
        else:
            unit_entity = unit_sei_id_entity_map[item["unit_id"]]
        if (
            ComponentConstPropertyValue.objects.filter(
                component=component_entity,
                property=const_property_entity,
            ).first()
            != None
        ):
            continue
        if item["value"] == "":
            continue

        component_constproperty_value_entity = ComponentConstPropertyValue(
            component=component_entity,
            property=const_property_entity,
            property_value_float=float(item["value"]),
            property_unit=unit_entity,
        )
        current_block.append(component_constproperty_value_entity)
        if len(current_block) >= block_size:
            blocks.append(current_block)
            current_block = []
    if current_block != []:
        blocks.append(current_block)

    block_num = len(blocks)
    for index in range(block_num):
        if index < -1:
            continue
        try:
            with transaction.atomic():
                ComponentConstPropertyValue.objects.bulk_create(blocks[index])
                print(index)
        except Exception as e:
            traceback.print_exc()
            print("error in block:", index)
            return


def convert_dippt_T_data_to_json():
    unit_file_path = (
        "files/basic_data_init_final_v2/dippr/5.Tdep Data Sets-20250306R1.xlsx"
    )
    # unit_file_path = "files/basic_data_init_final_v2/DIPPR对齐表/3.constTest.xlsx"
    sheet_name = "Sheet1"
    df = pd.read_excel(unit_file_path, sheet_name=sheet_name, header=0, dtype=str)
    start_row = 0
    end_row = 54
    # end_row = 13
    num_columns = 18
    const_property_data_list = []
    selected_data = df.iloc[start_row:, :num_columns].fillna("")
    for index, row in selected_data.iterrows():
        const_property_data_item = {}
        const_property_data_item["DIPPR_ID"] = row["DIPPR ID"]
        const_property_data_item["Compound"] = row["Compound"]
        const_property_data_item["Property"] = row["Property"]
        const_property_data_item["ePropAbbreviation"] = row["ePropAbbreviation"]
        const_property_data_item["sPhaseSymbol"] = row["sPhaseSymbol"]
        const_property_data_item["sPhaseSymbol2"] = row["sPhaseSymbol2"]
        const_property_data_item["T_value"] = row["T"]
        const_property_data_item["property_value"] = row["Value"]
        const_property_data_item["unit_id"] = row["nUnitIDCombine"]
        const_property_data_item["Error"] = row["Error"]
        const_property_data_item["Note"] = row["Note"]
        const_property_data_item["dippr_ref_id"] = row["Ref"]
        const_property_data_item["Acceptance"] = row["Acceptance"]
        const_property_data_item["Data_Type"] = row["Data Type"]
        const_property_data_list.append(const_property_data_item)

    with open("files/basic_data_init_final_v2/property_T_data.json", "w") as json_file:
        json.dump(const_property_data_list, json_file, indent=4)


def convert_dippt_t_data_to_group_json():
    note_json_path = "files/basic_data_init_final_v2/property_T_data.json"
    with open(note_json_path, "r", encoding="utf-8") as file:
        dippr_t_property_data_list = json.load(file)

    # 进行分组，获取所有的单位id和物性缩写 ，相态符号，数据类型，
    unit_id_set = set()
    property_abb_set = set()
    phase_symbol_set = set()
    data_type_set = set()

    groups = defaultdict(list)
    for item in tqdm(dippr_t_property_data_list, desc="Processing t property data"):
        key = (
            item["DIPPR_ID"],
            item["ePropAbbreviation"],
            item["sPhaseSymbol"],
            item["sPhaseSymbol2"],
            item["dippr_ref_id"],
            item["Acceptance"],
        )
        groups[key].append(item)
        if item["unit_id"] != "":
            unit_id_set.add(item["unit_id"])
        if item["ePropAbbreviation"] != "":
            property_abb_set.add(item["ePropAbbreviation"])
        if item["sPhaseSymbol"] != "":
            phase_symbol_set.add(item["sPhaseSymbol"])
        if item["sPhaseSymbol2"] != "":
            phase_symbol_set.add(item["sPhaseSymbol2"])
        if item["Data_Type"] != "":
            data_type_set.add(item["Data_Type"])

    # 将分组结果转换为字典形式（可选）
    grouped_dict = {f"Group_{'_'.join(key)}": group for key, group in groups.items()}
    sum_dict = {
        "unit_id_list": list(unit_id_set),
        "ePropAbbreviation_list": list(property_abb_set),
        "sPhaseSymbol_list": list(phase_symbol_set),
        "Data_Type_list": list(data_type_set),
        "grouped_data": grouped_dict,
    }

    # 将分组结果存储到JSON文件
    output_file = "files/basic_data_init_final_v2/grouped_property_T_data.json"
    with open(output_file, "w", encoding="utf-8") as f:
        json.dump(sum_dict, f, indent=4, ensure_ascii=False)

    print(f"分组结果已存储到 {output_file}")


def init_dippr_t_data():
    super_admin_entity = User.objects.filter(user_account="superadmin").first()

    note_json_path = "files/basic_data_init_final_v2/grouped_property_T_data.json"
    with open(note_json_path, "r", encoding="utf-8") as file:
        grouped_property_T_data = json.load(file)

    unit_id_list = grouped_property_T_data["unit_id_list"]
    ePropAbbreviation_list = grouped_property_T_data["ePropAbbreviation_list"]
    sPhaseSymbol_list = grouped_property_T_data["sPhaseSymbol_list"]
    Data_Type_list = grouped_property_T_data["Data_Type_list"]
    grouped_data = grouped_property_T_data["grouped_data"]
    # 获取note
    note_json_path = "files/basic_data_init_final_v2/dippr_note.json"
    with open(note_json_path, "r", encoding="utf-8") as file:
        note_dict = json.load(file)
    # 获取dippr与ref对齐
    dippr_ref_2_ref_id_dict_path = (
        "files/basic_data_init_final_v2/dippr_id_2_ref_id.json"
    )
    with open(dippr_ref_2_ref_id_dict_path, "r", encoding="utf-8") as file:
        dippr_ref_2_ref_id_dict = json.load(file)
    # dippr id 属性
    dippr_attribute_entity = Attribute.objects.filter(name_en="nDipprID").first()
    if dippr_attribute_entity is None:
        print("dippr attribute not exist")
        return
    # 温度的变量
    variable_temperature_entity = PropertyVariable.objects.filter(symbol="T").first()
    if variable_temperature_entity is None:
        print("variablei temperature not exist")
        return
    # 温度的单位
    unit_K_entity = Unit.objects.filter(sei_id="U-9-1").first()
    if unit_K_entity is None:
        print("unit symbol K not exist")
        return

    # 获取所有的单位并作映射
    unit_entity_list = Unit.objects.filter(valid_flag=True, sei_id__in=unit_id_list)
    unit_sei_id_entity_map = {}
    for unit_entity in unit_entity_list:
        if unit_entity.sei_id != "":
            unit_sei_id_entity_map[unit_entity.sei_id] = unit_entity
    if len(unit_entity_list)!=len(unit_id_list):
        print("unit match error!")
        return

    # 获取物性的映射表
    property_entity_list = Property.objects.filter(
        valid_flag=True, abbreviation__in=ePropAbbreviation_list
    )
    property_abb_entity_map = {}
    for property_entity in property_entity_list:
        if property_entity.abbreviation != "":
            property_abb_entity_map[property_entity.abbreviation] = property_entity
    if len(property_entity_list)!=len(ePropAbbreviation_list):
        print("property match error!")
        return


    # 补丁
    if Phase.objects.filter(abbreviation="V1").first() is None:
        phase_entity = Phase.objects.create(
            name="V1(未命名)",
            name_en="V1()",
            abbreviation="V1",
            is_pure_used=True,
            is_binary_used=True,
            is_ternary_used=True,
            is_multi_used=True,
            is_reaction_used=True,
            is_complex_used=True,
            create_person=super_admin_entity,
            modify_person=super_admin_entity,
        )

    # 获取相态映射表
    phase_entity_list = Phase.objects.filter(abbreviation__in=sPhaseSymbol_list)
    phase_symbol_entity_map = {}
    for phase_entity in phase_entity_list:
        if phase_entity.abbreviation != "":
            phase_symbol_entity_map[phase_entity.abbreviation] = phase_entity

    if len(phase_entity_list)!=len(sPhaseSymbol_list):
        print(phase_entity_list)
        print(sPhaseSymbol_list)
        print("phase match error!")
        return
    
    
    # 获取或者创建data type
    data_type_entity_map = {}
    for data_type in Data_Type_list:
        if data_type == "Not Specified":
            data_type = "Unspecified"
        if data_type == "Experiment":
            data_type = "Experimental" 
        data_present_way_entity = DataPresentWay.objects.filter(name=data_type).first()
        if data_present_way_entity == None:
            data_present_way_entity = DataPresentWay.objects.create(
                name=data_type,
                creator=super_admin_entity,
                last_editor=super_admin_entity,
            )
        data_type_entity_map[data_type] = data_present_way_entity
    # 数据类型：Unknown
    data_present_unknow_entity = DataPresentWay.objects.filter(name="Unknown").first()
    if data_present_unknow_entity == None:
        data_present_unknow_entity = DataPresentWay.objects.create(
            name="Unknown", creator=super_admin_entity, last_editor=super_admin_entity
        )
    purpose_others_entity = ExpPurpose.objects.filter(name="others").first()
    if purpose_others_entity == None:
        purpose_others_entity = ExpPurpose.objects.create(
            name="others",
            creator=super_admin_entity,
            last_editor=super_admin_entity,
        )

    print("extra data finish")

    for index,(key, item) in enumerate(tqdm(grouped_data.items(), desc="Processing const property data")):
        try:
            if index<-1:
                continue
            item_first = item[0]
            # 处理数据集的内容
            # 组分
            component_entity_attribute_map = ComponentAttributeValue.objects.filter(
                attribute=dippr_attribute_entity,
                attribute_value_char=item_first["DIPPR_ID"],
            ).first()
            if component_entity_attribute_map is None:
                print("error at component dippr id :", item_first["DIPPR_ID"])
                continue
            else:
                component_entity = component_entity_attribute_map.componentidentifier
            # 相态
            phase_entity_dataset_list = []
            if item_first["sPhaseSymbol"] != "":
                phase_entity1 = phase_symbol_entity_map[item_first["sPhaseSymbol"]]
                phase_entity_dataset_list.append(phase_entity1)
            if item_first["sPhaseSymbol2"] != "":
                phase_entity2 = phase_symbol_entity_map[item_first["sPhaseSymbol2"]]
                phase_entity_dataset_list.append(phase_entity2)
            # 物性
            property_entity = property_abb_entity_map[item_first["ePropAbbreviation"]]
            # 物性单位
            if item_first["unit_id"] == "":
                raise Exception()
            property_unit_entity = unit_sei_id_entity_map[item_first["unit_id"]]
            # 获取note
            description = ""
            comment = ""
            if item_first["Note"] != "":
                note1 = note_dict.get(item_first["Note"], "")
                if note1 != "":
                    description += note1 + "\n"
                    comment += note1 + "\n"
            if item_first["Acceptance"] != "":
                comment += "Acceptance" + item_first["Acceptance"]
            # 定义数据类型
            data_type = item_first["Data_Type"]
            if data_type == "Not Specified":
                data_type = "Unspecified"
            if data_type == "Experiment":
                data_type = "Experimental" 
            if data_type == "":
                # 数据类型为Unknown
                data_present_way_entity = data_present_unknow_entity
            else:
                data_present_way_entity = data_type_entity_map[data_type]
            # 获取文献
            if item_first["dippr_ref_id"] == "":
                dippr_reference_entity = None
            else:
                ref_id = dippr_ref_2_ref_id_dict.get(item_first["dippr_ref_id"], "")
                if ref_id == "":
                    dippr_reference_entity = None
                else:
                    dippr_reference_entity = Literature.objects.filter(
                        liter_code=str(ref_id)
                    ).first()
            # 构造一个数据集名称,名称+物性
            dataset_name = (
                item_first["Compound"]
                + "--"
                + item_first["Property"]
                + "--"
                + "Temperature"
            )

            with transaction.atomic():
                # 构造一个数据集
                dataset_entity = ExpDataset.objects.create(
                    name=dataset_name,
                    zh_name=dataset_name,
                    description=description,
                    comment=comment,
                    source_liter=dippr_reference_entity,
                    purpose=purpose_others_entity,
                    data_present_way=data_present_way_entity,
                    review_status=1,
                    creator=super_admin_entity,
                    last_editor=super_admin_entity,
                )
                # 设置数据集的相态
                if len(phase_entity_dataset_list) != None:
                    dataset_entity.phases.set(phase_entity_dataset_list)
                # 设置组分
                ExpDatasetComponentMap.objects.create(
                    exp_dataset=dataset_entity, component=component_entity
                )
                # 设置数据集物性
                ExpDatasetPropertyMap_entity = ExpDatasetPropertyMap.objects.create(
                    exp_dataset=dataset_entity,
                    property=property_entity,
                    unit=property_unit_entity,
                    measurement_error_percent=True,
                )
                # 设置变量
                ExpDatasetVariableMap_entity = ExpDatasetVariableMap.objects.create(
                    exp_dataset = dataset_entity,
                    variable = variable_temperature_entity,
                    unit=unit_K_entity
                )
                # 要创建多个数据点了
                datapoint_entity_list = []
                datapoint_property_list  = []
                datapoint_variable_list  = []
                for point_item in item:
                    datapoint_entity1 = DataPoint()
                    datapoint_entity_list.append(datapoint_entity1)
                    if point_item["T_value"]!="":
                        datapoint_variable_entity = DataPointExpDatasetVariableMap(
                            data_point = datapoint_entity1,
                            exp_dataset_variable_map = ExpDatasetVariableMap_entity,
                            value = float(point_item["T_value"])
                        )
                        datapoint_variable_list.append(datapoint_variable_entity)
                    if point_item["property_value"]!="":
                        if point_item["Error"]!="":
                            datapoint_property_entity = DataPointExpDatasetPropertyMap(
                                data_point=datapoint_entity1,
                                exp_dataset_property_map=ExpDatasetPropertyMap_entity,
                                value=point_item["property_value"],
                                measurement_error_value=point_item["Error"]
                            )
                        else:
                            datapoint_property_entity = DataPointExpDatasetPropertyMap(
                                data_point=datapoint_entity1,
                                exp_dataset_property_map=ExpDatasetPropertyMap_entity,
                                value=point_item["property_value"],
                            )
                        datapoint_property_list.append(datapoint_property_entity)
                DataPoint.objects.bulk_create(datapoint_entity_list)
                DataPointExpDatasetVariableMap.objects.bulk_create(datapoint_variable_list)
                DataPointExpDatasetPropertyMap.objects.bulk_create(datapoint_property_list)
                dataset_entity.data_points.set(datapoint_entity_list)

        except Exception as e:
            print("error at index:",index)
            print("error at group:", key)
            traceback.print_exc()
            return


def init_dippr_const_content():

    convert_note_to_json()
    convert_dippr_id_2_ref_id_json()
    init_dippr_const_data()
    convert_dippr_const_property_data_to_json()
    init_dippr_const_property()
    init_dippr_const_property_data()
    convert_dippt_T_data_to_json()
    convert_dippt_t_data_to_group_json()
    init_dippr_t_data()

    print("dippr const data finish")
